package qq

import (
	"gitee.com/edodo/flysdk/api"
)

func init() {
	api.RegisterBaseMpApi(api.QQ, &qqMpApi{})
}

type qqMpApi struct {
}

func (qApi *qqMpApi) GetStableAccessToken(config *api.MpConfig, b bool) (*api.AccessToken, error) {
	return nil, api.ErrUnsupportedApi
}

func (qApi *qqMpApi) GetPhoneNumber(token *api.AccessToken, s string) (*api.PhoneNumber, error) {
	return nil, api.ErrUnsupportedApi
}

// https://q.qq.com/wiki/develop/miniprogram/server/open_port/port_use.html
func (qApi *qqMpApi) GetAccessToken(config *api.MpConfig) (at *api.AccessToken, e error) {
	// get access token by http request
	p := map[string]string{
		"grant_type": "client_credential",
		"appid":      config.AppId,
		"secret":     config.AppSecret,
	}
	output := new(QqAccessToken)
	if e = api.Get(ACCESS_TOKEN_URL, p, output); e != nil {
		//glog.Errorf("Get access token failed: %v", e)
		return
	}
	at, e = output.ToAccessToken()
	return
}

// https://q.qq.com/wiki/develop/miniprogram/server/open_port/port_login.html#code2session
func (qApi *qqMpApi) Code2Session(config *api.MpConfig, at *api.AccessToken, js_code, anonymous_code string) (acs *api.Code2Session, e error) {
	p := map[string]string{
		"grant_type": "authorization_code",
		"appid":      config.AppId,
		"secret":     config.AppSecret,
		"js_code":    js_code,
	}
	output := new(QqCode2Session)
	if e = api.Get(JSCODE_SESSION_URL, p, output); e != nil {
		//glog.Errorf("code2Session failed: %v", e)
		return
	}
	acs = output.Code2Session
	return

}
